require("dplyr")

## Set working directory
## to Dataverse folder

survey <- read.delim("Harris_Data/Harris 2000 National Issues Survey, study no. 13422/harris_s13422_spss.txt", header = TRUE)


# pid
survey$pid <- survey$ID 

# study 
survey$study <- "13422"

# study year (year)
survey$year <- 2000

# geographic data (urban)
table(survey$Q17)
survey$urban <- dplyr::recode(survey$Q17,
                       `1` = "Urban",
                       `2` = "Suburban",
                       `3` = "Rural")
table(survey$urban)

# geographic data (region)
table(survey$REGION)
survey$region <- dplyr::recode(survey$REGION,
                        `1` = "East",
                        `2` = "East",
                        `3` = "South",
                        `4` = "South",
                        `5` = "Midwest",
                        `6` = "Midwest",
                        `7` = "West",
                        `8` = "West")
table(survey$region)

# respondent head of household (hh)
survey$hh <- NA

# increasing inequality (inequality)
table(survey$Q206A2)
survey$inequality <- dplyr::recode(as.character(survey$Q206A2),
                            `1` = "Feel",
                            `0` = "Don't Feel",
                            `-8` = "Not Sure",
                            `-9` = "Refused")
#survey$inequality[!is.na(survey$inequality) & survey$inequality == "Refused"] <- NA
table(survey$inequality)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

# union (union.self)
survey$union.self <- NA
survey$union.other <- NA

# employment (employed)
survey$employed <- NA

# empl self
survey$employed.self <- NA

# occupation
survey$occupation <- NA

# occ self
survey$occupation.self <- NA

# household size (hhsize)
## note this is just the count of adults, don't include count of children
## and don't have variable for that
table(survey$Q2005)
survey$hhsize <- as.character(survey$Q2005)
survey$hhsize[survey$hhsize < 0] <- NA
table(survey$hhsize)

# education (educ)
table(survey$Q2020)
survey$educ <- dplyr::recode(survey$Q2020,
                      `1` = "Less than high school",
                      `2` = "Completed some high school",
                      `3` = "High school graduate",
                      `4` = "Some college",
                      `5` = "College graduate",
                      `6` = "Some graduate school",
                      `7` = "Completed post graduate",
                      `-8` = "Don't know",
                      `-9` = "Refused")
table(survey$educ)

# household income (income)
table(survey$Q2050)
table(survey$Q2052)
table(survey$Q2053)

## create special variable to indicate "don't know"/"refused" on initial question
survey$Q2052[survey$Q2050 < 0] <- -100
survey$Q2053[survey$Q2050 < 0] <- -100

## take the income data only for those making less than $50k
survey$income <- dplyr::recode(survey$Q2052,
                        `1` = "$0 to $14,999",
                        `2` = "$15,000 to $24,999",
                        `3` = "$25,000 to $34,999",
                        `4` = "$35,000 to $49,999",
                        `-8` = "Not sure",
                        `-9` = "Refused",
                        `-99.99` = "Over $50,000",
                        `-100` = "Disregard") # disregarding DK/refused
table(survey$income)

## combine with income data for those making more than $50k
## do this by only using the points where <$50k is NOT available
survey$income[survey$income == "Over $50,000"] <- survey$Q2053[survey$Q2052 == -99.99]
table(survey$income)

## recode >$50k data
survey$income <- dplyr::recode(survey$income,
                        `1` = "$50,000 to $74,999",
                        `2` = "$75,000 to $99,999",
                        `3` = "$100,000 to $124,999",
                        `4` = "$125,000 to $149,999",
                        `5` = "$150,000 to $199,999",
                        `6` = "$200,000 to $249,999",
                        `7` = "$250,000 or more",
                        `-8` = "Not sure",
                        `-9` = "Refused")
table(survey$income)

## get rid of initial data that was supposed to be disregarded
survey$income[survey$income == "Disregard"] <- NA
table(survey$income)

# age
table(survey$Q2013)
survey$age <- as.character(survey$Q2013)
survey$age[survey$age < 0] <- NA
table(survey$age)

# race
table(survey$Q2070)
survey$race1 <- dplyr::recode(survey$Q2070,
                      `1` = "White",
                      `2` = "Black",
                      `3` = "African-American",
                      `4` = "Asian or Pacific Islander",
                      `5` = "American Indian or Alaskan native",
                      `6` = "Some other race",
                      `-8` = "Not sure",
                      `-9` = "Refused")
survey$race2 <- dplyr::recode(survey$Q2060,
                              `-9` = "Decline/not sure",
                              `-8` = "Decline/not sure",
                              `1` = "Yes, hispanic",
                              `0` = "No, not hispanic")
table(survey$race1)
table(survey$race2)
survey$race <- ifelse(survey$race1 == "Refused" |
                        survey$race1 == "Not sure", 
                      "Decline/not sure", ifelse(survey$race1 == "White",
                                                 ifelse(survey$race2 == "Decline/not sure",
                                                        "Decline/not sure",
                                                        ifelse(survey$race2 == "No, not hispanic",
                                                               "Non-Hispanic White",
                                                               "Hispanic White")), "Non-white"))
table(survey$race)
table(survey$race[survey$race1 == "White"])

# politics (party)
table(survey$Q2043)
survey$party <- dplyr::recode(survey$Q2043,
                       `1` = "Republican",
                       `2` = "Democrat",
                       `3` = "Independent",
                       `4` = "Other",
                       `-8` = "Not sure",
                       `-9` = "Refused")
table(survey$party)

# politics (ideology)
table(survey$Q2045)
survey$ideology <- dplyr::recode(survey$Q2045,
                          `1` = "Conservative",
                          `2` = "Moderate",
                          `3` = "Liberal",
                          `-8` = "Not sure",
                          `-9` = "Refused")
table(survey$ideology)

# gender
table(survey$Q503)
survey$gender <- dplyr::recode(survey$Q503,
                        `1` = "Male",
                        `2` = "Female")
table(survey$gender)

# religion
survey$religion <- NA

#factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- dplyr::recode(survey$Q206A1,
                                 `1` = "Feel",
                                 `0` = "Don't Feel",
                                 `-8` = "Not Sure",
                                 `-9` = "Refused")
survey$dontcount <- dplyr::recode(survey$Q206A3,
                                  `1` = "Feel",
                                  `0` = "Don't Feel",
                                  `-8` = "Not Sure",
                                  `-9` = "Refused")
survey$leftout <- dplyr::recode(survey$Q206A4,
                                `1` = "Feel",
                                `0` = "Don't Feel",
                                `-8` = "Not Sure",
                                `-9` = "Refused")

##
survey$question_place <- "before party"

# subset
survey_13422 <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                          "inequality", "inequality.variable", "union.self", "union.other",
                          "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                          "age", "race", "party", "ideology", "gender", "religion",
                          "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                          "question_place")]


# save file
#saveRDS(survey_13422, file = "Harris_Data/survey_13422.rds")
